from PyQt5.QtWidgets import QDialog, QFormLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QHBoxLayout, QCheckBox, \
    QSpacerItem
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt

from core.auth import AuthService
from database.db_session import get_db



class RegisterDialog(QDialog):
    def __init__(self, app):
        super().__init__()
        self.app = app
        self.initUI()

    def initUI(self):
        self.setWindowTitle("用户注册")
        self.resize(500, 350)
        icon = QIcon("E:/pythonwork/personal_schedule_system/rsc/icon.jpg")
        self.setWindowIcon(icon) # 设置窗口图标

        # 使用 QFormLayout 布局
        form_layout = QFormLayout()
        form_layout.setVerticalSpacing(20)  # 设置行间垂直间距为20像素
        form_layout.setContentsMargins(40, 30, 40, 30)
        form_layout.setSpacing(15)

        # 创建用户名输入框
        self.username_input = QLineEdit()
        self.username_input.setPlaceholderText("请输入用户名")
        self.username_input.setFont(QFont("Arial", 10))  # 设置字体
        form_layout.addRow(QLabel("用户名"), self.username_input)

        # 创建密码输入框
        self.password_input = QLineEdit()
        self.password_input.setPlaceholderText("请输入密码")
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setFont(QFont("Arial", 10))
        form_layout.addRow(QLabel("密码"), self.password_input)

        self.confirm_input = QLineEdit()
        self.confirm_input.setPlaceholderText("请再次输入密码")
        self.confirm_input.setEchoMode(QLineEdit.Password)
        form_layout.addRow(QLabel("确认密码"), self.confirm_input)

        form_layout.addItem(QSpacerItem(20, 20))  # 固定高度间隔

        # 创建按钮布局
        button_layout = QHBoxLayout()
        self.register_btn = QPushButton("注册")
        self.register_btn.clicked.connect(self.try_register)
        self.register_btn.setFont(QFont("Arial", 10))
        self.register_btn.setStyleSheet("background-color: #4CAF50; color: white;")  # 设置按钮样式
        # 可以为注册按钮添加回车快捷键
        self.register_btn.setShortcut("Return")

        self.back_btn = QPushButton("返回登录")
        self.back_btn.clicked.connect(self.back_to_login)
        self.back_btn.setFont(QFont("Arial", 10))
        self.back_btn.setStyleSheet("background-color: #8787ff; color: white;")

        button_layout.addWidget(self.register_btn)
        button_layout.addWidget(self.back_btn)

        # 将按钮布局添加到表单布局中
        form_layout.addRow(button_layout)

        # 设置整体布局
        self.setLayout(form_layout)

        # 设置样式表
        self.setStyleSheet("""
            QDialog {
                background-color: qlineargradient(
                    spread:pad, x1:0, y1:0, x2:1, y2:1,
                    stop:0 #e0f7fa, stop:1 #ffffff
                );
                border-radius: 12px;
                border: 1px solid rgba(255, 255, 255, 0.3);
            }

            QLabel {
                font-family: "Microsoft YaHei";
                font-size: 10pt;
                color: #2c3e50;;
            }

            QLineEdit {
                font-family: "Microsoft YaHei";
                font-size: 8pt;
                color: #333;
                background-color: #ffffff;
                border: 1px solid #cccccc;
                border-radius: 8px;
                padding: 6px;
                height:40px;
            }

            QLineEdit:focus {
                border: 1px solid #0097FF;
                background-color: #e6f7ff;
            }
            
             QLineEdit::placeholder {
                color: #95a5a6;
                font-style: italic;
            }

            QPushButton {
                background-color: #4CAF50;
                color: white;
                border: none;
                border-radius: 6px;
                padding: 10px;
                font-size: 8pt;
                font-weight: bold;
                height: 30px;
            }

            QPushButton:hover {
                background-color: #45a049;
            }

            QPushButton:pressed {
                background-color: #3e8e41;
            }

            QCheckBox {
                font-family: "Microsoft YaHei";
                font-size: 13px;
                color: #555;
                padding-left: 5px;
            }

            QMessageBox {
                background-color: qlineargradient(
                    spread:pad, x1:0, y1:0, x2:1, y2:1,
                    stop:0 #e0f7fa, stop:1 #ffffff
                );
                border: 2px solid #C0C0C0; /* 边框样式 */
                border-radius: 5px; /* 圆角 */
                font-size: 8pt; /* 字体大小 */
            }

            QMessageBox QLabel {
                color: #333333; /* 文本颜色 */
                font-size: 10pt; /* 文本字体大小 */
            }
        """)


    def try_register(self):
        username = self.username_input.text().strip()
        password = self.password_input.text().strip()
        confirm = self.confirm_input.text().strip()

        if not username or not password:
            QMessageBox.warning(self, "提示", "请输入用户名和密码")
            return

        if password.strip() != confirm.strip():
            QMessageBox.warning(self, "提示", "两次密码不一致")
            return

        try:
            db = next(get_db())
            result = AuthService.register(db, username, password)
        finally:
            db.close()  # 确保会话关闭
        if result:
            QMessageBox.information(self, "注册成功", f"请登录")
            self.back_to_login()
        else:
            QMessageBox.critical(self, "注册失败", "用户名已存在")

    def back_to_login(self):
        self.close()
        from ui.login_dialog import LoginDialog
        self.app.login_window = LoginDialog(self.app)
        self.app.login_window.show()